﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CFM.API;
using CFM.DTO;
using CFM.Common;
using System.Text.RegularExpressions;

namespace CFM
{
    public partial class frmProvider : Form
    {
        private ProviderAPI _api;
        public frmProvider()
        {
            InitializeComponent();
            _api = new ProviderAPI();
        }

        private void txtProviderName_TextChanged(object sender, EventArgs e)
        {
            if (txtProviderName.Enabled == true&&txtProviderID.Enabled==true)
            {
                txtProviderID.Text =  MethodHelper.GetIdFromName(txtProviderName.Text);
            }
        }

        private void frmProvider_Load(object sender, EventArgs e)
        {
            EnableControl(false);

            btnDelete.Enabled = false;
            btnEdit.Enabled = false;
            btnSave.Enabled = false;

            BindDataGridView();
        }

        private void BindDataGridView()
        {
            grvProvider.DataSource = _api.GetAll();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            EnableControl(true);
            ResetControl();

            btnSave.Enabled = true;
            btnDelete.Enabled = false;
            btnEdit.Enabled = false; 
        }

        private void grvProvider_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0)
                return;
            Provider pro = grvProvider.Rows[e.RowIndex].DataBoundItem as Provider;
            txtProviderID.Text = pro.ProviderId;
            txtProviderName.Text = pro.ProviderName;
            txtDescription.Text = pro.Description;
            txtAddress.Text = pro.Address;
            txtPhone.Text = pro.Phone;
            EnableControl(false);
            MethodHelper.ClearErrorProvider();

            btnDelete.Enabled = true;
            btnEdit.Enabled = true;
            btnSave.Enabled = false;
        }

        private bool CheckBeforeSave()
        {
            bool flag = true;
            if (txtProviderID.Enabled && txtProviderID.Text.Length == 0)
            {
                MethodHelper.AddErrorProvider(txtProviderID, Const.EMPTY_MESSAGE);
                flag = false;
            }
            if (txtProviderName.Enabled && txtProviderName.Text.Length == 0)
            {
                MethodHelper.AddErrorProvider(txtProviderName, Const.EMPTY_MESSAGE);

                flag = false;
            }
            if (!txtProviderName.Enabled && !txtProviderID.Enabled)
                flag = false;
            return flag;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckBeforeSave())
                return;
            Provider pro = new Provider();
            pro.ProviderId = txtProviderID.Text;
            pro.ProviderName = txtProviderName.Text;
            pro.Description = txtDescription.Text;
            pro.Address = txtAddress.Text;
            pro.Phone = txtPhone.Text;

            if (txtProviderID.Enabled)
                _api.Create(pro);
            else
                _api.Update(pro);
            BindDataGridView();
            ResetControl();
            MethodHelper.ClearErrorProvider();

            btnDelete.Enabled = false;
            btnEdit.Enabled = false;
        }

        private void ResetControl()
        {
            txtProviderID.Text = string.Empty;
            txtProviderName.Text = string.Empty;
            txtDescription.Text = string.Empty;
            txtAddress.Text = string.Empty;
            txtPhone.Text = string.Empty;
         
            btnSave.Enabled = true;
            btnDelete.Enabled = false;
            btnEdit.Enabled = false;
            txtProviderName.Focus();
            MethodHelper.ClearErrorProvider();
        }
        private void EnableControl(bool flag)
        {
            txtProviderID.Enabled = flag;
            txtProviderName.Enabled = flag;
            txtDescription.Enabled = flag;
            txtAddress.Enabled = flag;
            txtPhone.Enabled = flag;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (txtProviderID.Text.Length > 0)
            {
                _api.Delete(txtProviderID.Text.Trim());
                BindDataGridView();
                ResetControl();

                btnDelete.Enabled = false;
                btnEdit.Enabled = false;
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (txtProviderID.Text.Length > 0)
            {
                EnableControl(true);
                txtProviderID.Enabled = false;
                MethodHelper.ClearErrorProvider();

                btnSave.Enabled = true;
            }
        }
    }
}
